package RoomPanel;

import houseHold.House;
import houseHold.Point2D;

import java.awt.Color;
import java.awt.Rectangle;
import java.util.Vector;

import javax.swing.BorderFactory;

public class Aproach {
	public static boolean isEnabled = true;

	public static synchronized void aproach(RoomPanel current,
			Vector<RoomPanel> others) {
		System.out.println("currentsize=" + current.getSize());
		Rectangle CurrenR = current.getBounds();
		int minH = 1000, minW = 1000, minHi = -1, minWi = -1;
		for (int i = 0; i < others.size(); i++) {
			if (others.get(i).getPanelID() == current.getPanelID()) {
			} else {
				Rectangle nextR = others.get(i).getBounds();
				Rectangle intersection = CurrenR.intersection(nextR);
				if (intersection.height < minH && intersection.height > -5) {
					minH = intersection.height;
					minHi = i;
				}
				if (intersection.width < minW && intersection.width > -5) {
					minW = intersection.width;
					minWi = i;
				}
			}
		}

		if (minH <= minW)
			if (minH <= 5 && minH > -5) {
				validateH(current, others.get(minHi), minH);
			}
		if (minW < minH)
			if (minW <= 5 && minW > -5) {
				validateW(current, others.get(minWi), minW);
			}
		computeCount(others);

	}

	public static void computeCount(Vector<RoomPanel> others) {

		if (others.size() > 1) {
			for (int i = 0; i < others.size(); i++) {
				others.get(i).setBorder(
						BorderFactory.createLineBorder(Color.red, 3));
				others.get(i).paint = 0;
				others.get(i).setMarked(false);
			}
			for (int i = 0; i < others.size(); i++) {
				Rectangle r1 = others.get(i).getBounds();
				for (int j = 0; j < others.size(); j++) {
					if (i != j) {
						Rectangle r2 = others.get(j).getBounds();
						Rectangle intersects = r1.intersection(r2);
						int res = intersects.height * intersects.width;
						if (res == 0 && intersects.height >= 0
								&& intersects.width >= 0
								&& !others.get(i).isMarked()) {
							others.get(i).setBorder(
									BorderFactory.createLineBorder(Color.green,
											3));
							others.get(i).paint = 1;
							others.get(i).setMarked(true);
						} else if (res > 0 && intersects.height >= 0
								&& intersects.width >= 0) {
							others.get(i).setBorder(
									BorderFactory
											.createLineBorder(Color.red, 3));
							others.get(i).paint = 0;
							others.get(i).setMarked(true);
						}
					}

				}

			}
		} else if (others.size() == 1) {
			others.get(0).setBorder(
					BorderFactory.createLineBorder(Color.green, 3));
			others.get(0).setMarked(true);
			others.get(0).paint = 1;
		}
		for (int i = 0; i < others.size(); i++) {
			others.get(i).getRoom().setOrigin(
					new Point2D(others.get(i).getX(), others.get(i).getY()));
		}
		int count = 0;
		for (int i = 0; i < others.size(); i++) {
			count += others.get(i).paint;
		}
		System.out.println(count + " = count");
		if (count >= others.size() && count > 0) {
			House.getInstance().setActive(true);
		} else
			House.getInstance().setActive(false);

	}

	private static synchronized void validateH(RoomPanel current,
			RoomPanel next, int height) {

		if (height > 0)
			height *= (-1);
		Rectangle currentR = current.getBounds();
		current.setBounds(currentR.x, currentR.y + height, currentR.width,
				currentR.height);

	}

	private static synchronized void validateW(RoomPanel current,
			RoomPanel next, int width) {

		if (width > 0)
			width *= (-1);
		Rectangle currentR = current.getBounds();
		current.setBounds(currentR.x + width, currentR.y, currentR.width,
				currentR.height);

	}

}
